<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->middleware('guest')->except('logout');
    }

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        //return flase;

        $name = $request->name;
        $password = $request->password;


//        if (Auth::guard('user')->attempt(['name' => $name, 'password' => $password, 'is_forbided' => 0])) {
//            // Authentication passed...
//            //var_dump(['email' => $email, 'password' => $password, 'is_forbided' => 0]);
//
//            $this->validate($request, [
//                'email' => 'required|email|exists:users,email,is_forbided,0',
//                'password' => 'required',
//            ]);
//            User::where('name', $name)->update(['updated_at' => date('Y-m-d H:i:s')]);
//            return redirect()->intended('dashboard');
//        }
    }

    public function username(){
        return 'name';
    }

    protected function validateLogin(Request $request)
    {
//        dump($request);
//        die();
        $this->validate($request, [
            $this->username() => 'required|exists:users,' . $this->username() . ',is_forbided,0',
            'password' => 'required',
        ], [
            $this->username() . '.exists' => '您的账户被禁用或不存在此账户，请联系管理员.'
        ]);
    }



    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function login(Request $request)
    {

        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {

            $user = User::where('name', $request->name);
//            dump($user);
//            die();
            // 获取最后登录时间
//            $request->session()->put('last_login_time', $user->last_login_time);
            // 更新最后登录时间
            $user->update(['last_login_time' => time()]);

            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
}
